package com.yuke.cloud.service.uac.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.api.uac.dto.AccEmployeeInfoDto;
import com.yuke.cloud.service.api.wmc.dto.WarehouseInfoDto;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.uac.dao.AccEmployeeGroupMapper;
import com.yuke.cloud.service.uac.dto.UserGroupDto;
import com.yuke.cloud.service.uac.entity.AccEmployeeGroup;
import com.yuke.cloud.service.uac.entity.AccUser;
import com.yuke.cloud.service.uac.service.AccEmployeeGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by zn on 2019/11/26.
 */
@Service
@Transactional
public class AccEmployeeGroupServiceImpl extends BaseService<AccEmployeeGroup> implements AccEmployeeGroupService {
    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;
    @Resource
    private AccEmployeeGroupMapper accEmployeeGroupMapper;
    @Resource
    private SmcFeignApi smcFeignApi;

    @Override
    public UserGroupDto selectEmployeeGroupInfoByUserId(Long userId) {
        UserGroupDto userGroupDto = accEmployeeGroupMapper.selectEmployeeGroupInfoByUserId(userId);
        Long groupId =0L;
        if (userGroupDto!=null){
            groupId = userGroupDto.getGroupId();
//            if (userGroupDto.getLevel().toString().equals("3")){
//                groupId = userGroupDto.getParentId();
//            }
            if (groupId>1){
                System.out.println("进入这里"+groupId);
                List<WarehouseInfoDto> warehouseInfoDto = wmcStorageFeignApi.getWarehouseInfoByGroupId(groupId);
                System.out.println("到这里"+warehouseInfoDto);
                if (warehouseInfoDto != null && warehouseInfoDto.size()>0){
                    userGroupDto.setWarehouseId(warehouseInfoDto.get(0).getWarehouseId());
                    userGroupDto.setWarehouseName(warehouseInfoDto.get(0).getWarehouseName());
                }
            }else{
                userGroupDto.setWarehouseId(1L);
                userGroupDto.setWarehouseName("全国总仓");
            }

        }else{
            //可能是店家，作废
//            UserGroupDto userGroup = new UserGroupDto();
//            List<ShopInfoDto> shopInfoDtos = smcFeignApi.getShopInfoDtoByUserId(userId);
//            if (shopInfoDtos!=null && shopInfoDtos.size()>0 ){
//                Long shopId = shopInfoDtos.get(0).getShopId();
//                List<Long> shopList = new ArrayList<>();
//                shopList.add(shopId);
//                List<Map> maps = wmcStorageFeignApi.getWarehouseName(shopList);
//                logger.info("@@@@@@@@@@@@@@@@@@2---------maps={}",maps);
//                System.out.println(maps);
//                if (maps != null && maps.size()>0) {
//                    if (!maps.get(0).isEmpty() && maps.get(0).get("warehouseId") != null){
//                        userGroup.setUserId(userId);
//
//                        userGroup.setWarehouseId(Long.valueOf(maps.get(0).get("warehouseId").toString()));
//                        userGroup.setWarehouseName(maps.get(0).get("warehouseName").toString());
//                    }
//
//                }
//            }

        }

        return userGroupDto;
    }

    @Override
    public  AccUser selectUserInfoByUserId(Long userId){
        return accEmployeeGroupMapper.selectUserInfoByUserId(userId);
    }

    @Override
    public  List<AccEmployeeInfoDto> selectEmployInfo(AccEmployeeInfoDto accEmployeeInfoDto){
        return accEmployeeGroupMapper.selectEmployInfo(accEmployeeInfoDto);
    }

}
